package com.lottery.ruok.lottrey.dialog;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.eventbusObject.FingerSet;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;

/**
 * Created by ASUS on 2017/8/2.
 */

public class FingerPrintDialog extends Activity {
    private int type = 0;

    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.layout)
    RelativeLayout layout;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        startFingerPrintListener();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finger_layout);

        ButterKnife.bind(this);

        if(getIntent() != null) {
            type = getIntent().getIntExtra("type", 0);
            if (type == 0) {
                cancel.setVisibility(View.GONE);
                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)layout.getLayoutParams();
                layoutParams.height = Utils.dip2px(getApplicationContext(), 180);
                layout.setLayoutParams(layoutParams);
            }
        }

        startFingerPrintListener();

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(type == 1){
                    FingerSet fingerSet = new FingerSet();
                    fingerSet.setType(1);
                    EventBus.getDefault().post(fingerSet);
                } else if(type == 2){
                    FingerSet fingerSet = new FingerSet();
                    fingerSet.setType(2);
                    EventBus.getDefault().post(fingerSet);
                }

                finish();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public class MyAuthenticationCallback extends FingerprintManager.AuthenticationCallback{
        private static final String TAG = "AuthenticationCallback";

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            Log.d(TAG, "onAuthenticationSucceeded: " + "校验成功");
            if(type == 1){
                UserInfo.setFingerLock(true);
            } else if(type == 2){
                UserInfo.setFingerLock(false);
            }
            finish();
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            Log.d(TAG, "onAuthenticationFailed: " + "校验失败");
            Utils.showToast("验证失败");
        }

        @Override
        public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
//            super.onAuthenticationHelp(helpCode, helpString);
//            Log.d(TAG, "onAuthenticationHelp: " + helpString);
        }

        @Override
        public void onAuthenticationError(int errorCode, CharSequence errString) {
//            super.onAuthenticationError(errorCode, errString);
//            Log.d(TAG, "onAuthenticationError: " + errString);
        }
    }

    /**
     * 开始指纹校验
     */
    @SuppressLint("NewApi")
    private void startFingerPrintListener() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED) {
                FingerprintManager manager = (FingerprintManager) getSystemService(Activity.FINGERPRINT_SERVICE);
                CancellationSignal mCancellationSignal = new CancellationSignal();
                //指纹检验
                manager.authenticate(null, mCancellationSignal, 0, new MyAuthenticationCallback(), null);
            }
        }
    }

    @Override
    public void onBackPressed() {

    }
}
